{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Multiple dispatch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we'll explore **multiple dispatch**, which is a key feature of Julia.\n",
    "\n",
    "Multiple dispatch makes software *generic* and *fast*!\n",
    "\n",
    "#### Starting with the familiar\n",
    "\n",
    "To understand multiple dispatch in Julia, let's start with what we've already seen.\n",
    "\n",
    "We can declare functions in Julia without giving Julia any information about the types of the input arguments that function will receive:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(x) = x^2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and then Julia will determine on its own which input argument types make sense and which do not:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f([1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Specifying the types of our input arguments\n",
    "\n",
    "However, we also have the *option* to tell Julia explicitly what types our input arguments are allowed to have.\n",
    "\n",
    "For example, let's write a function `foo` that only takes strings as inputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(x::String, y::String) = println(\"My inputs x and y are both strings!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We see here that in order to restrict the type of `x` and `y` to `String`s, we just follow the input argument name by a double colon and the keyword `String`.\n",
    "\n",
    "Now we'll see that `foo` works on `String`s and doesn't work on other input argument types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(\"hello\", \"hi!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To get `foo` to work on integer (`Int`) inputs, let's tack `::Int` onto our input arguments when we declare `foo`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(x::Int, y::Int) = println(\"My inputs x and y are both integers!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(3, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now `foo` works on integers! But look, `foo` also still works when `x` and `y` are strings!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "foo(\"hello\", \"hi!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is starting to get to the heart of multiple dispatch. When we declared\n",
    "\n",
    "```julia\n",
    "foo(x::Int, y::Int) = println(\"My inputs x and y are both integers!\")\n",
    "```\n",
    "we didn't overwrite or replace\n",
    "```julia\n",
    "foo(y::String, y::String)```\n",
    "\n",
    "Instead, we just added an additional ***method*** to the ***generic function*** called `foo`.\n",
    "\n",
    "A ***generic function*** is the abstract concept associated with a particular operation.\n",
    "\n",
    "For example, the generic function `+` represents the concept of addition.\n",
    "\n",
    "A ***method*** is a specific implementation of a generic function for *particular argument types*.\n",
    "\n",
    "For example, `+` has methods that accept floating point numbers, integers, matrices, etc.\n",
    "\n",
    "We can use the `methods` to see how many methods there are for `foo`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "methods(foo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "methods(+)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, we now can call `foo` on integers or strings. When you call `foo` on a particular set of arguments, Julia will infer the types of the inputs and dispatch the appropriate method. *This* is multiple dispatch.\n",
    "\n",
    "Multiple dispatch makes our code generic and fast. Our code can be generic and flexible because we can write code in terms of abstract operations such as addition and multiplication, rather than in terms of specific implementations. At the same time, our code runs quickly because Julia is able to call efficient methods for the relevant types.\n",
    "\n",
    "To see which method is being dispatched when we call a generic function, we can use the @which macro:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@which foo(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@which 3.0 + 3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given that a method written specifically for floating point numbers is dispatched on `3.0 + 3.0`, the LLVM code generated is extremely terse:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@code_llvm 3.0 + 3.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Note that Julia is fast even when we write generic function definitions because, at the end of the day, specific/tailored methods are called under the hood.\n",
    "\n",
    "For example, note that we can declare the adding function `myadd` without providing any type annotations - "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "myadd(x, y) = x + y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and though we haven't constrained the types of `x` and `y`, we'll see that the LLVM code generated for `myadd(3.0, 3.0)` looks like that of `3.0 + 3.0`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "@code_llvm myadd(3.0, 3.0)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 1.0.0",
   "language": "julia",
   "name": "julia-1.0"
  },
  "language": "Julia",
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.0.0"
  },
  "toc": {
   "nav_menu": {
    "height": "119px",
    "width": "251px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": "2",
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
